package com.chao.config;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.servlet.*;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;
import java.util.Map;

/**
 * @Author:PengChao
 * @Description:
 * @Date: Created in 14:33 2021/7/30
 * @Modified By:
 */
public class PasswordFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {

        String password = request.getParameter("password");
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(password);
        MyHttpServletRequestWrapper wrapper = new MyHttpServletRequestWrapper((HttpServletRequest) request);
        wrapper.setParameter("password",encode);
        filterChain.doFilter(wrapper,response);
    }

    @Override
    public void destroy() {

    }
}
